package player.ui.tree;

import java.awt.Component;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.filechooser.FileSystemView;

import org.jvnet.substance.api.renderers.SubstanceDefaultTreeCellRenderer;


	public class SubstanceFileTreeCellRenderer extends SubstanceDefaultTreeCellRenderer {

		private static final long serialVersionUID = 1900882575094021697L;

		protected static FileSystemView fsv = FileSystemView.getFileSystemView();
		private Map<String, Icon> iconCache = new HashMap<String, Icon>();

		private Map<File, String> rootNameCache = new HashMap<File, String>();

		public Component getTreeCellRendererComponent(JTree tree, Object value,
				boolean sel, boolean expanded, boolean leaf, int row,
				boolean hasFocus) {
			if ((value instanceof File)){
				File file = (File)value;
				String filename = "";
				if (file != null) {
					if (false) {
						filename = rootNameCache.get(file);
						if (filename == null) {
							filename = fsv.getSystemDisplayName(file);
							rootNameCache.put(file, filename);
						}
					} else {
						filename = fsv.getSystemDisplayName((File) value);
					}
				}
				JLabel result = (JLabel) super.getTreeCellRendererComponent(tree,
						filename, sel, expanded, leaf, row, hasFocus);
				if (file != null) {
					Icon icon = iconCache.get(filename);
					if (icon == null) {
						icon = fsv.getSystemIcon(file);
						iconCache.put(filename, icon);
					}
					result.setIcon(icon);
				}
				return result;
			}else{
				return this;
			}
			
		}
	}